var checker = {
	
	/**
	 * array to store check function
	 */
	__checkFunction__ : [],
	
	/**
	 * array to store debug info
	 */
	__debugInfo__ : [],
	
	/**
	 * show debug info or not
	 */
	__debug__ : true,
	
	/**
	 * array to store check result
	 * format of a result:
	 * {
	 		title : String
	 		view : "grid" | "list"
	 		items : []
 		}
 	 * format of a result item:
 	  {
 	  		level : "error" | "warning" | "info"
 	  		detail : HTML
 	  }
 	}
	 */
	__checkResult__ : [],
	
	/**
	 * start to check
	 */
	run : function(){
		this.__checkResult__ = [];
		this.__debugInfo__ = [];
		var container = content.document.getElementById("WedCheckerResult");
		if(container){
			container.innerHTML = "";
		}
		this.renderResult();
		for(var i in this.__checkFunction__){
			this.__checkFunction__[i]();
		}
		this.showDebug();
	},
	
	/**
	 * add a check function to checker
	 */
	addCheckFunction : function(func){
		this.__checkFunction__.push(func);
	},
	
	/**
	 * add a check result to checker
	 */
	addCheckResult : function(result){
		this.__checkResult__.push(result);
		var container = content.document.getElementById("checkerbd");
		var div = content.document.createElement("div");
		div.setAttribute("class","open");
		
		var html = "<h3 onclick='javascript:toggleResult(this)'>" + result.title + "</h3>";
		html += "<ul class='" + result.view + "'>";
		for(var j in result.items){
			var item = result.items[j];
			html += "<li class='" + item.level + "'>" + item.detail + "</li>";
		}
		html += "</ul>";
		div.innerHTML = html;
		container.appendChild(div);
	},
	
	/**
	 * add a check result to checker
	 */
	debug: function(info){
		this.__debugInfo__.push(info);
	},
	
	/**
	 * Render result
	 */
	renderResult : function(){
		//init result container
		var container = content.document.getElementById("WedCheckerResult");
		if(!container){
			container = content.document.createElement("div");
			container.setAttribute("id", "WedCheckerResult");
			content.document.body.appendChild(container);
		}
		
		//init result style
		var resultStyleContainer =  content.document.getElementById("resultStyleContainer");
		if(!resultStyleContainer){
			resultStyleContainer = content.document.createElement("style");
			resultStyleContainer.setAttribute("id", "resultStyleContainer");
			var pageStyle = checkerUtil.readFile("chrome://wedchecker/skin/resultStyle.css");
			resultStyleContainer.innerHTML = pageStyle;
			content.document.body.appendChild(resultStyleContainer);
		}
		
		//init result script
		var resultScriptContainer =  content.document.getElementById("resultScriptContainer");
		if(!resultScriptContainer){
			resultScriptContainer = content.document.createElement("script");
			resultScriptContainer.setAttribute("id", "resultScriptContainer");
			var pageScript = checkerUtil.readFile("chrome://wedchecker/content/resultScript.js");
			resultScriptContainer.innerHTML = pageScript;
			content.document.body.appendChild(resultScriptContainer);
		}
		
		var html = "";
		html += "<div class='checkerhd'><span><a href='#' onclick='closeResult();return false'>X</a></span><h2>检查结果</h2></div>";
		html += "<div class='checkerbd' id='checkerbd'>";
		html += "</div>";
		container.innerHTML = html;
		container.style.height = (content.innerHeight-160) + "px";
		container.style.width = (content.innerWidth-240) + "px";
		content.document.getElementById("checkerbd").style.maxHeight = (content.innerHeight-195) + "px";
		container.style.left = "120px";
		container.style.top ="80px";
	},
	
	/**
	 * show debug info
	 */
    showDebug: function() {
        if(this.__debug__){
        	var debugContainer = content.document.getElementById("debugContainer");
        	if(!debugContainer){
        		debugContainer = content.document.createElement("ul");
        		debugContainer.setAttribute("id", "debugContainer");
        		content.document.body.appendChild(debugContainer);
        	}
        	for(var i in this.__debugInfo__){
        		var item = content.document.createElement("li");
        		item.innerHTML = this.__debugInfo__[i];
        		debugContainer.appendChild(item);
        	}
        }
    },
	
	/**
	 * show about dialogue
	 */
    showAbout: function() {
        alert("WED Checker\n按照规范对页面进行检查的工具");
    },

	/**
       * show help diagloue
       */
	showHelp: function() {
        var win = window.top.getBrowser().selectedBrowser.contentWindow;
        win.open("http://wed.baidu.com");
    }
};

